package com.nf.health.app.activity.my.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.PictureActivity;
import com.nf.health.app.activity.my.bean.QueryHistory;
import com.nf.health.app.glide.GlideUtil;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 恩福 on 2016/9/28.
 */

public class QueryHitoryListAdapter extends RecyclerView.Adapter<QueryHitoryListAdapter.MyViewHolder>{
    private Context context;
    private OnItemDelClickListener mOnItemClickLitener;
    private boolean isOpen; // 换成Bean类中的 否则复用出错
    private List<QueryHistory> ecgHistoryList;
    private int screenWidth = 0;
    public QueryHitoryListAdapter(Context context, List<QueryHistory> ecgHistoryList) {
        this.context = context;
        this.ecgHistoryList = ecgHistoryList;
        screenWidth =  DensityUtil.getScreenIntWidth(context);
    }
    public void setEcgHitoryListAdapterDataChanged(List<QueryHistory> ecgHistoryList) {
        this.ecgHistoryList = ecgHistoryList;
        notifyDataSetChanged();
    }
    @Override
    public QueryHitoryListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.ecg_list_item,null);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final QueryHistory queryHistory = ecgHistoryList.get(position);
         if(queryHistory.isOpen() == true || position ==0){
             holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
             holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
             queryHistory.setOpen(true);
             holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowup);
        }else{
             holder.ll_show_ecg_content.setVisibility(View.GONE);
        }
        holder.iv_ecg_triangle.setVisibility(View.VISIBLE);
        holder.iv_ecg_del.setVisibility(View.GONE);

        GlideUtil.loadNetImg2(context,holder.tv_ecg_pic,queryHistory.getImg(),R.drawable.default_square_image);
        holder.tv_ecg_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                ArrayList<String> urls = new ArrayList<String>();
                urls.add(queryHistory.getImg());
                bundle.putStringArrayList("imgUrls",urls);
                bundle.putInt("position", 0);
                bundle.putInt("type", PictureActivity.TYPE_NET_IMG);
                ActivityUtils.showActivity(context, PictureActivity.class, bundle);
                urls.clear();
            }
        });
             holder.iv_ecg_triangle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(queryHistory.isOpen()) {//去关闭
                        holder.ll_show_ecg_content.setVisibility(View.GONE);
                        holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowdown);
                        queryHistory.setOpen(false);
                     }else {//去打开
                        holder.ll_show_ecg_content.setVisibility(View.VISIBLE);
                        queryHistory.setOpen(true);
                        holder.iv_ecg_triangle.setImageResource(R.drawable.iv_blue_arrowup);
                    }
                }
            });
        String time = "";
        if(TextUtils.isEmpty(queryHistory.getDate())){
            time =   "未知时间";
        }else{
            time = DateUtil.date2String(Long.parseLong(queryHistory.getDate()),"yyyy-MM-dd HH:mm:ss");
        }
        holder.tv_ecg_time.setText(time);
        holder.iv_ecg_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickLitener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        int i = ecgHistoryList == null ? 0 : ecgHistoryList.size();
        return i;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_ecg_tip;
        ImageView tv_ecg_pic;

        LinearLayout ll_show_ecg_content;
        RelativeLayout rl_content_close1;
        ImageView iv_ecg_triangle;
        ImageView iv_ecg_del;//go to delete

        TextView tv_ecg_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_ecg_pic =  (ImageView) itemView.findViewById(R.id.tv_ecg_pic);
            ll_show_ecg_content = (LinearLayout) itemView.findViewById(R.id.ll_show_ecg_content);
            rl_content_close1 = (RelativeLayout) itemView.findViewById(R.id.rl_content_close1);
            tv_ecg_time = (TextView) itemView.findViewById(R.id.tv_ecg_time);
            iv_ecg_triangle = (ImageView) itemView.findViewById(R.id.iv_ecg_triangle);
            tv_ecg_tip = (TextView) itemView.findViewById(R.id.tv_ecg_tip);
            iv_ecg_del = (ImageView) itemView.findViewById(R.id.iv_ecg_del);
        }
    }


    //监听事件
    public interface OnItemDelClickListener
    {
        void onItemClick(int position);
    }


    public void setOnItemClickLitener(OnItemDelClickListener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

}
